<?php

/*
  / Function: count_all
  / Counts All the Records in a table
  / Use:  echo count_all('your_table_name');
  / BetaTiger Framework
 */
function get_bro($id){
	$sql=mysql_query("SELECT * FROM `mll_relationships` WHERE `indivisual_1_id`='".$id."' AND `relationship_type_code`='4'")or die(mysql_error());
	while($row=mysql_fetch_array($sql)){
	  $bro.=$row['indivisual_2_id'].",";
	  $bro.=get_bro($row['indivisual_2_id']).",";
	}
	return $bro;
}
function get_all_bro($id){ $bro='';
	$sql2 = mysql_query("SELECT `indivisual_1_id` FROM `mll_relationships` WHERE `indivisual_2_id`='".$id."' AND `relationship_type_code`='4'") or die(mysql_error()); 
    if (mysql_num_rows($sql2)) { 
		$row=mysql_fetch_array($sql2); $bro.=$row['indivisual_1_id'].",";
		$bro.=get_bro($row['indivisual_1_id']).",";
    }else{
		$bro.=get_bro($id).",";
	}
	$bro.=get_all_bro($id).",";
	return $bro;	
}

function generateUsername() {
    $characters = "0123456789abcdefghijklmnopqrstuvwxyz";
    $string = "";
    for ($x = 0; $x < 6; $x++):
        $string .= $characters[mt_rand(0, strlen($characters))];
    endfor;
    return $string;
}

function generatePassword() {
    $characters = "0123456789abcdefghijklmnopqrstuvwxyz";
    $string = "";
    for ($x = 0; $x < 8; $x++):
        $string .= $characters[mt_rand(0, strlen($characters))];
    endfor;
    return $string;
}

function pr($arr, $die = true) {
    echo "<pre>";
    print_r($arr);
    echo "</pre>";
    if ($die) {
        die();
    }
}

function count_all($table_name) {
    $q = "SELECT * FROM $table_name";
    $qr = mysql_query($q) or die("Error: FN Count_all");
    $qn = mysql_num_rows($qr);
    return $qn;
}

function count_rowall($query) {
    $qr = mysql_query($query) or die("Error: FN count_rowall");
    $qn = mysql_num_rows($qr);
    return $qn;
}

/*
  / Function: delete_only
  / Deletes the Records in a table where condition is applied
  / Use:  Execute - delete_only(your_table_name, Where, What);
  / BetaTiger Framework
 */

function delete_only($table_name, $where, $what) {
    $q = "DELETE FROM $table_name WHERE $where=$what LIMIT 1";
    $qr = mysql_query($q) or die("Error: FN Delete_Only");
    return $qr;
}

/*
  / Function: count_only
  / Counts All the Records in a table where condition is applies
  / Use:  echo count_only(your_table_name, Where, What);
  / BetaTiger Framework
 */

function count_only($table_name, $where, $what) {
    $q = "SELECT * FROM $table_name WHERE $where = $what";
    $qr = mysql_query($q) or die("Error: FN Count_all");
    $qn = mysql_num_rows($qr);
    return $qn;
}

/*
  / Function: show_error_nodelete
  / Display Error if cant be deleted
  / Use:  echo show_error_nodelete();
  / BetaTiger Framework
 */

function show_error_nodelete() {
    $err = "Error: Can't be deleted, This item is in use!";
    return $err;
}

/*
  / Function: cut_text
  / Display Results after limiting the text
  / Use:  cut_text($cut_it, $cut_on);
  / BetaTiger Framework
 */

function cut_text($cut_it, $cut_on) {
    if (strlen(trim($cut_it)) > $cut_on) {
        echo substr($cut_it, 0, strpos($cut_it, " ", $cut_on));
        echo "...";
    } else {
        echo "$cut_it";
    }
}

/*
  / Function: get_item
  / Get a single item from a table when
  / Use:  $result=function();
  / BetaTiger Framework
 */

function get_item($table_name, $where, $what, $getit) {
    $gq = "SELECT * FROM $table_name WHERE $where=$what";
    $gr = @mysql_query($gq);
    $tr = @mysql_fetch_array($gr);
    return $tr[$getit];
}

function get_allitem($table_name, $where, $what) {
    $gq = "SELECT * FROM $table_name WHERE $where=$what";
    $gr = @mysql_query($gq);
    $tr = @mysql_fetch_array($gr);
    return $tr;
}

function get_result($gq) {
    $gr = @mysql_query($gq);
    $tr = @mysql_fetch_array($gr);
    return $tr;
}

function insert($query) {
    $gr = mysql_query($query) or die(mysql_error());
    return $id = mysql_insert_id();
}



function get_relationship_type_code($relation) {
    switch ($relation) {
        case "Father":
            $code = 1;
            break;
        case "Mother":
            $code = 1;
            break;
        case "Husband":
            $code = 3;
            break;
        case "Wife":
            $code = 3;
            break;
        case "Brother":
            $code = 4;
            break;
        case "Sister":
            $code = 4;
            break;
        case "Son":
            $code = 2;
            break;
        case "Daughter":
            $code = 2;
            break;
        case "Spouse":
            $code = 3;
            break;
    }
    return $code;
}

function reverse_relation($relation1, $gender) {
    if ($gender == 'male') {
        if (($relation1 == 'Father') || ($relation1 == 'Mother')) {

            $relation2 = 'Son';
            return $relation2;
        }
        if (($relation1 == 'Son') || ($relation1 == 'Daughter')) {

            $relation2 = 'Father';
            return $relation2;
        }

        if ($relation1 == 'Wife') {
            $relation2 = 'Husband';
            return $relation2;
        }
        if ($relation1 == 'Ex-Wife') {
            $relation2 = 'Ex-Husband';
            return $relation2;
        }
        if (($relation1 == 'Sister') || ($relation1 == 'Brother')) {
            $relation2 = 'Brother';
            return $relation2;
        }

        if (($relation1 == 'Uncle') || ($relation1 == 'Aunt')) {
            $relation2 = 'Nephew';
            return $relation2;
        }
        if (($relation1 == 'Nephew') || ($relation1 == 'Neice')) {
            $relation2 = 'Uncle';
            return $relation2;
        }
        if (($relation1 == 'Grand-Father') || ($relation1 == 'Grand-Mother')) {
            $relation2 = 'Grand-Son';
            return $relation2;
        }
        if (($relation1 == 'Father-in-law') || ($relation1 == 'Mother-in-law')) {
            $relation2 = 'Son-in-law';
            return $relation2;
        }
        if (($relation1 == 'Son-in-law') || ($relation1 == 'Daughter-in-law')) {
            $relation2 = 'Father-in-law';
            return $relation2;
        }
        if (($relation1 == 'Brother-in-law') || ($relation1 == 'Sister-in-law')) {
            $relation2 = 'Brother-in-law';
            return $relation2;
        }
    } else {
        if (($relation1 == 'Father') || ($relation1 == 'Mother')) {

            $relation2 = 'Daughter';
            return $relation2;
        }
        if (($relation1 == 'Son') || ($relation1 == 'Daughter')) {

            $relation2 = 'Mother';
            return $relation2;
        }

        if ($relation1 == 'Husband') {
            $relation2 = 'Wife';
            return $relation2;
        }
        if ($relation1 == 'Ex-Husband') {
            $relation2 = 'Ex-Wife';
            return $relation2;
        }
        if (($relation1 == 'Sister') || ($relation1 == 'Brother')) {
            $relation2 = 'Sister';
            return $relation2;
        }
        if (($relation1 == 'Uncle') || ($relation1 == 'Aunt')) {
            $relation2 = 'Neice';
            return $relation2;
        }
        if (($relation1 == 'Nephew') || ($relation1 == 'Neice')) {
            $relation2 = 'Aunt';
            return $relation2;
        }
        if (($relation1 == 'Grand-Father') || ($relation1 == 'Grand-Mother')) {
            $relation2 = 'Grand-Daughter';
            return $relation2;
        }
        if (($relation1 == 'Father-in-law') || ($relation1 == 'Mother-in-law')) {
            $relation2 = 'Daughter-in-law';
            return $relation2;
        }
        if (($relation1 == 'Son-in-law') || ($relation1 == 'Daughter-in-law')) {
            $relation2 = 'Mother-in-law';
            return $relation2;
        }
        if (($relation1 == 'Brother-in-law') || ($relation1 == 'Sister-in-law')) {
            $relation2 = 'Sister-in-law';
            return $relation2;
        }
    }
}

function get_roll_code($relation) {
    $gr = mysql_query("SELECT * `mll_roles` WHERE `role_description`='" . $relation . "'") or die(mysql_query());
    $row = mysql_fetch_array($gr);
    return $row['role_code'];
}

function father_form($id) {
    $frm = "<div id='father' style='line-height: 0; top: 38px;left: 1px;position: absolute;z-index: 9999;'> <img src='images/male.png' width='82'>
      <div style='width: 200px;margin-left: -53px;margin-top: 10px;position:relative;z-index: 5; text-align: center;'>Add Your Father</div>
    </div>";
    $frm.="<div id='father_form' style='display: block;'>
      <div id='form_div' style='float: left; position: absolute;float: right;line-height: 10px;'>
        <form action='' id='father_frm'>
          <input type='hidden' name='relation' id='relation' value='Father'>
          <input type='hidden' name='id' id='id' value='" . $id . "'>
          <input type='hidden' name='gender' id='gender' value='male'>
          <table width='310' border='0' style='font-size: 10px;'>
            <tbody>
              <tr>
                <td><div align='left'><strong style='font-size:16px;'>Father's Name</strong></div></td>
                <td><label style=' font-size: 12px; '>
                    <input type='radio' name='live' value='L' checked='checked'>
                    Living</label>
                  <label style=' font-size: 12px; '>
                    <input type='radio' name='live' value=''>
                    Deceased</label></td>
              </tr>
              <tr>
                <td width='50%'><div align='left'>
                    <input type='text' name='fname' id='fname' value='' class='form_input' placeholder='First Name' required>
                  </div></td>
                <td width='50%'><input type='text' name='lname' id='father_form_lname' class='form_input' value='' placeholder='Last Name' required='required'></td>
              </tr>
              <tr>
                <td><div align='left'>
                    <input type='text' name='email' class='form_input' value='' placeholder='Email or Send Invite'>
                  </div>
                  <label for='textfield'>
                  <div align='left'><!--<strong><u>Add from address book</u></strong>--></div>
                  </label></td>
                <td><img src='images/cancel.png' width='71' height='22' id='cancelShare' class='cursor' />
                  
                  <!--<input type='button' name='submit' src='images/add.png' width='70' height='23'  class='cursor'/> -->
                  <a href='javascript:void(0);' data-type='submit' onClick='submit_form(this);'><img src='images/add.png' width='70' height='23'></a>
                </td>
              </tr>
            </tbody>
          </table>
        </form>
      </div>
    </div>";
    return $frm;
}

function father_div($id) {
    $sql = mysql_query("SELECT * FROM `mll_relationships` WHERE `indivisual_1_id`='" . $id . "' AND `indivisual_2_role_code`=1 ") or die(mysql_error());
    $row = mysql_fetch_array($sql);

    $frm = "<div id='father_div'>" .
            get_parents_image($row['indivisual_2_id'], get_item('mll_indivisuals', 'indivisual_id', $row['indivisual_2_id'], 'gender'), get_item('mll_indivisuals', 'indivisual_id', $row['indivisual_2_id'], 'image'))
            . " <div style=' float: left; '>
            <p>" . get_item('mll_indivisuals', 'indivisual_id', $row['indivisual_2_id'], 'first_name') . " " . get_item('mll_indivisuals', 'indivisual_id', $row['indivisual_2_id'], 'last_name') . "</p>
            <input type='text' name='email' value='' placeholder='Enter Email' class='form_input2'>
            <p>Edit  |  Tree  |  More <img src='images/down.png'></p>
        </div>
    </div>";
    return $frm;
}

function mother_form($id) {
    $frm = "<div id='mother' style='top: 36px;line-height: 0;position: absolute;z-index: 9999;'> <img src='images/femail.png' width='82' style='float:left;'>
      <div style='width: 200px;margin-left: -56px;margin-top: 93px;position: relative;z-index: 5; text-align: center;'>Add Your Mother</div>
    </div>
    <div id='mother_form' style='display: block;'>
      <div id='form_div' style='float: right;line-height: 10px;width:308px;'>
        <form action='' method='post' id='mother_frm'>
          <input type='hidden' name='relation' id='relation' value='Mother'>
          <input type='hidden' name='id' id='id' value='" . $id . "'>
          <input type='hidden' name='gender' id='gender' value='female'>
          <table width='310' border='0' style='font-size: 10px;'>
            <tbody>
              <tr>
                <td><div align='left'><strong style='font-size:16px;'>Mother's Name</strong></div></td>
                <td><label style=' font-size: 12px; '>
                    <input type='radio' name='live' value='L' id='mother_form_live1' checked='checked'>
                    Living</label>
                  <label style=' font-size: 12px; float: right; '>
                    <input type='radio' name='live' value='' id='mother_form_live2'>
                    Deceased</label></td>
              </tr>
              <tr>
                <td width='50%'><div align='left'>
                    <input type='text' name='fname' value='' id='mother_form_fname' class='form_input' placeholder='First Name'>
                  </div></td>
                <td width='50%'><div align='right'>
                    <input type='text' name='lname' value='singh' id='mother_form_lname' class='form_input' placeholder='Last Name'>
                  </div></td>
              </tr>
              <tr>
                <td><div align='left'>
                    <img src='images/cancel.png' width='71' height='22' id='cancelShare1'/>
                    <a href='javascript:void(0);' onClick='submit_form(this);'><img src='images/add.png' width='70' height='23'></a> </div></td>
                <td><div align='right'>
                    <input type='text' name='email' value='' id='mother_form_email' class='form_input' placeholder='Email or Send Invite'>
                  </div>
                  <label for='textfield'>
                  <div align='right'><!--<strong><u>Add from address book</u></strong>--></div>
                  </label></td>
              </tr>
            </tbody>
          </table>
        </form>
      </div>
    </div>";

    return $frm;
}

function mother_div($id) {
    //$div="<div id='mother_div'> 
//      <div style='float:right ;margin:5px;'>
//          <a href='javascript:void();'><img src='images/femail1.png' style='width: 50px;border: 2px solid grey;float:left;'></a>   
//      </div> 
//      <div style='float:right;'>              
//          <p>mother</p>
//          <input type='text' name='email' id='email' value='' placeholder='Enter Email' class='form_input2' />
//          <p>Edit  |  Tree  |  More <img src='images/down.png'  /></p>
//	  </div>
//	</div>";	
//	return $div; 

    $sql = mysql_query("SELECT * FROM `mll_relationships` WHERE `indivisual_1_id`='" . $id . "' AND `indivisual_2_role_code`=2 ") or die(mysql_error());
    $row = mysql_fetch_array($sql);
    $div = "<div id='mother_div'> " . get_parents_image($row['indivisual_2_id'], get_item('mll_indivisuals', 'indivisual_id', $row['indivisual_2_id'], 'gender'), get_item('mll_indivisuals', 'indivisual_id', $row['indivisual_2_id'], 'image')) . "
      <div style='float:right;'>              
         <p>" . get_item('mll_indivisuals', 'indivisual_id', $row['indivisual_2_id'], 'first_name') . " " . get_item('mll_indivisuals', 'indivisual_id', $row['indivisual_2_id'], 'last_name') . "</p>
          <input type='text' name='email' id='email' value='' placeholder='Enter Email' class='form_input2' />
          <p>Edit  |  Tree  |  More <img src='images/down.png'  /></p>
	  </div>
	</div>";
    return $div;
}

//Get Brother SIster/////////////////////////////

//function get_brother_sister($id,$left,$right,$lr){
//	//$left=300; 
//  $top=268;
//  $sql=mysql_query("SELECT * FROM `mll_relationships` WHERE `indivisual_1_id`='".$id."' AND `relationship_type_code`='4'")or die(mysql_error());
//  while($row=mysql_fetch_array($sql)){ 
//   
//   $lr=$lr+1;
//   $lr_check=get_even_odd($lr);
//   
//  $subling=get_allitem('mll_indivisuals','indivisual_id',$row['indivisual_2_id']);
//  $a=find_wife_spouse($subling['indivisual_id'],$left,$right,$lr_check);
//// echo 'id='.$a[5].'And wife='.(260*$a[4]).'   ant its a'.$a[3].'</br>';
// //$return[3]=$lr_check;
// // $return[4]=$wife_count;
// // $return[5]=$id;
////  print_r( $a);
//  $left=$a[0];
//  $right=$a[2];
//  $div.=$a[1];
//  
//  if($lr_check=='ODD')
//  {
//  	$div.="<div class='window' new='".$lr."' id='window".$row['indivisual_2_id']."' style='left:".$left."px; top:".$top."px;  background:url(\"images/";
//  }
//  else
//  {
//	
//	 if(($row['indivisual_2_id']==$a[5])&&($a[4]!=0)){
//	 //if($row['indivisual_2_id']==11){
//		 $div.="<div class='window' new='".$lr."' id='window".$row['indivisual_2_id']."' style='right:".($right+260*$a[4])."px; top:".$top."px;  background:url(\"images/";
//		 //$subtr=260*$a[4];
//		 }else{ 
//		 
//		 
//	
//	$div.="<div class='window'  id='window".$row['indivisual_2_id']."' style='right:".($right)."px; top:".$top."px;  background:url(\"images/";}
//  }
//  if($subling['gender']=="male"){$div.="male_bg";}else{$div.="female_bgb";}
//  $div.=".png\") no-repeat;height: 37px;width: 161px;padding: 11px;margin: 0px auto;'>";
//  $count=count_rowall("SELECT * FROM `mll_relationships` WHERE `indivisual_1_id`='".$subling['indivisual_id']."'");
//  if($count==0){
//  $div.="<div id='delete' onClick='deleteuser(".$subling['indivisual_id'].");'><img src='images/x.png' alt='Delete' ></div>";
//  }
//   $div.=" <!--User Image-->
//    ".get_user_image($subling['indivisual_id'],$subling['image'])."
//    <!--User Image-->
//    <div id=\"name\">
//        <a href=\"javascript:void(0)\" title=";
//		//$parent=get_allitem('mll_indivisuals','indivisual_id',$row['indivisual_1_id']);
//		//if($subling['gender']=="male"){$div.="\"Brother of ".$parent['first_name']." ".$parent['last_name']."\" ";}else{$div.="\"Sister of ".$parent['first_name']." ".$parent['last_name']."\" ";}
//		$div.=" style=\"padding:0px;\">".substr($subling['first_name']." ".$subling['last_name'],0,15)."</a><br />
//         More <img src=\"images/aero-d.png\" /> 
//    </div>
//    <!-- right Arrow -->  
//    <div id=\"right_aero\" >
//    <a id=\"window1_right_arrow\" style=\"left: 180px;top: 8px;\" onClick=\"open_div(". $subling['indivisual_id'].",'". $subling['gender']."','right'); open_only(".$subling['indivisual_id'].",'right');\"></a>
//    </div>
//    <div id=\"right_form".$subling['indivisual_id']."\" style=\"float:left;\"></div>
//                                          
//     <!-- left Arrow -->  
//    <div id=\"left_aero\" >
//    <a id=\"window1_left_arrow\" style=\"left: -16px;top: 10px;\"  onclick=\"open_div(".$subling['indivisual_id'].",'". $subling['gender']."','left'); open_only(". $subling['indivisual_id'].",'left');\" ></a>
//    </div>
//    <div id=\"left_form".$subling['indivisual_id']."\" style=\"float:left;\"></div> 
//    
//    <!-- bootom arrow-->  
//	<div id=\"down_aero1\">
//    <a id=\"window1_down_arrow\" style=\"top: 58px;left: 73px;\" onclick=\"open_div(".$subling['indivisual_id'].",'".$subling['gender']."','bottom'); open_only(".$subling['indivisual_id'].",'bottom');\"></a>
//    </div>   
//    <div id=\"bottom_form".$subling['indivisual_id']."\" style=\"float:left;\"></div> 
//  </div>";
//  
//  $b=get_brother_sister($subling['indivisual_id'],$left-=130,$right-=130,$lr);
//  $left=$b[0];
//  $right=$b[3];
//  $div.=$b[1];
//  $lr=$b[2];
//
//  //$left=$left-250; 
//  
//  }	
//  $result[0]=$left;
//  $result[3]=$right;
//  $result[1]=$div;
//  $result[2]=$lr;
//  return $result;
//}
/////
function get_brother_sister($id,$left,$right,$lr){

  $cunt=1;
  $top=268;
  $array=all_b($id,$_SESSION['family_id']);
	  for($i=0;$i<(count($array));$i++){

  $lr_check=get_even_odd($cunt);
   
  $subling=get_allitem('mll_indivisuals','indivisual_id',$array[$i]);
  $a= get_wife_spouse($subling['indivisual_id'], $left,$right,$lr_check);
  
  $div.=$a[0];
  
  if($lr_check=='ODD')
  {
		$sub_l=260*$a[3]; 
		$left=$left-$sub_l;
		$div.="<div class='window' new='".$array[$i]."' id='window".$array[$i]."' style='left:".$left."px; top:".$top."px;  background:url(\"images/";
  } else {
	 if(($array[$i]==$a[5])&&($a[3]!=0)){
		$div.="<div class='window' new='".$array[$i]."' id='window".$array[$i]."' style='right:".($right+260*$a[3])."px; top:".$top."px;  background:url(\"images/";
	}else{ 
		 
		$sub_l=260*$a[3];
		
		$div.="<div class='window'  id='window".$array[$i]."' style='right:".($right)."px; top:".$top."px;  background:url(\"images/";}
		$right=$right-$sub_l;
	}
	
  if($subling['gender']=="male"){$div.="male_bg";}else{$div.="female_bgb";}
  $div.=".png\") no-repeat;height: 37px;width: 161px;padding: 11px;margin: 0px auto;'>";
  $count=count_rowall("SELECT * FROM `mll_relationships` WHERE `indivisual_1_id`='".$subling['indivisual_id']."'");
  if($count==0){
  $div.="<div id='delete' onClick='deleteuser(".$subling['indivisual_id'].");'><img src='images/x.png' alt='Delete' ></div>";
  }
   $div.=" <!--User Image-->
    ".get_user_image($subling['indivisual_id'],$subling['image'])."
    <!--User Image-->
    <div id=\"name\">
        <a href=\"javascript:void(0)\" title=";
		$div.=" style=\"padding:0px;\">".substr($subling['first_name']." ".$subling['last_name'],0,15)."</a><br />
         More <img src=\"images/aero-d.png\" /> 
    </div>
    <!-- right Arrow -->  
    <div id=\"right_aero\" >
    <a id=\"window1_right_arrow\" style=\"left: 180px;top: 8px;\" onClick=\"open_div(". $subling['indivisual_id'].",'". $subling['gender']."','right'); open_only(".$subling['indivisual_id'].",'right');\"></a>
    </div>
    <div id=\"right_form".$subling['indivisual_id']."\" style=\"float:left;\"></div>
                                          
     <!-- left Arrow -->  
    <div id=\"left_aero\" >
    <a id=\"window1_left_arrow\" style=\"left: -16px;top: 10px;\"  onclick=\"open_div(".$subling['indivisual_id'].",'". $subling['gender']."','left'); open_only(". $subling['indivisual_id'].",'left');\" ></a>
    </div>
    <div id=\"left_form".$subling['indivisual_id']."\" style=\"float:left;\"></div> 
    
    <!-- bootom arrow-->  
	<div id=\"down_aero1\">
    <a id=\"window1_down_arrow\" style=\"top: 58px;left: 73px;\" onclick=\"open_div(".$subling['indivisual_id'].",'".$subling['gender']."','bottom'); open_only(".$subling['indivisual_id'].",'bottom');\"></a>
    </div>   
    <div id=\"bottom_form".$subling['indivisual_id']."\" style=\"float:left;\"></div> 
  </div>";
  $cunt++; 
   if($lr_check=='ODD')
  {
 $left-=250;}else{
$right-=250;
 }
 }	
 $result[1]=$div;
 // $result[2]=$lr;
  return $result;
}
////
function chk_sibling($id) {
    $sql2 = mysql_query("SELECT `indivisual_1_id` FROM `mll_relationships` WHERE `indivisual_2_id`='" . $id . "' AND `relationship_type_code`='4'") or die(mysql_error());
    if (mysql_num_rows($sql2)) {
        $rows = mysql_fetch_array($sql2);
        $parent = $rows['indivisual_1_id'];
        $parent = chk_sibling($parent);
    } else {
        $parent = $id;
    }
    return $parent;
}

////
function chk_wife_spouse($id) {
    $sql2 = mysql_query("SELECT `indivisual_1_id` FROM `mll_relationships` WHERE `indivisual_2_id`='" . $id . "' AND `relationship_type_code`='3'") or die(mysql_error());
    if (mysql_num_rows($sql2)) {
        $rows = mysql_fetch_array($sql2);
        $parent = $rows['indivisual_1_id'];
        $parent = chk_wife_spouse($parent);
    } else {
        $parent = $id;
    }
    return $parent;
}

////Get Brother & SIster Connector///////////////
function get_bro_sis_conn($id) {
	    $con = "";
	$array=all_b($id,$_SESSION['family_id']);
	for($i=0;$i<(count($array));$i++){
		//}
//	
//
//    $sql = mysql_query("SELECT * FROM `mll_relationships` WHERE `indivisual_1_id`='" . $id . "' AND `relationship_type_code`='4'") or die(mysql_error());
//    while ($row = mysql_fetch_array($sql)) {

        //$test=chk_sibling($id);
        $con.="_addEndpoints(\"window" . $array[$i] . "\",  [\"TopCenter\", \"BottomCenter\" , \"RightMiddle\" ,\"LeftMiddle\"] , [\"LeftMiddle\" ,\"RightMiddle\", \"BottomCenter\" ]); jsPlumb.connect({uuids:[\"window".$id."TopCenter\",\"window" . $array[$i]. "TopCenter\"]});\n";
       //// $con.=find_wife_spouse_conn($row['indivisual_2_id']);
       // $con.=get_bro_sis_conn($row['indivisual_2_id']);
	   $con.=find_wife_spouse_conn($array[$i]);
    }
    return $con;
}

//Get Wife & Spouse /////////////////////////////
function get_wife_spouse($id, $left,$right,$ev_odd) {
    //$left=300; 

	//echo "SELECT * FROM `mll_relationships` WHERE `indivisual_1_id`='" . $id . "' AND `relationship_type_code`='3'";
    $top = 268;
    $sql = mysql_query("SELECT * FROM `mll_relationships` WHERE `indivisual_1_id`='" . $id . "' AND `relationship_type_code`='3'") or die(mysql_error());
    $wife=mysql_num_rows($sql);
	
    while ($row = mysql_fetch_array($sql)) {	$right=$right-250;
        $subling = get_allitem('mll_indivisuals', 'indivisual_id', $row['indivisual_2_id']);
        if($ev_odd=='ODD'){
        $div.="<div class='window' id='window" . $row['indivisual_2_id'] . "' style='left:" . $left . "px; top:" . $top . "px;  background:url(\"images/";
		}else{			
			 $div.="<div class='window' id='window" . $row['indivisual_2_id'] . "' style='right:" . $right . "px; top:" . $top . "px;  background:url(\"images/";
			}
        if ($subling['gender'] == "male") {
            $div.="male_bg";
        } else {
            $div.="female_bgb";
        }
        $div.=".png\") no-repeat;height: 37px;width: 161px;padding: 11px;margin: 0px auto;z-index: 9;'>
  <div id='delete' onClick='deleteuser(" . $subling['indivisual_id'] . ");'><img src='images/x.png' alt='Delete' ></div>
    <!--User Image-->
    " . get_user_image($subling['indivisual_id'], $subling['image']) . "
    <!--User Image-->
    <div id=\"name\">
        <a href=\"javascript:void(0)\" title=";
        if ($subling['gender'] == "male") {
            $div.="Husband";
        } else {
            $div.="Wife";
        }
        $div.=" style=\"padding:0px;\">" . substr($subling['first_name'] . " " . $subling['last_name'], 0, 15) . "</a><br />
         More <img src=\"images/aero-d.png\" /> 
    </div>
    <!-- right Arrow -->  
    <div id=\"right_aero\" >
    <a id=\"window1_right_arrow\" style=\"left: 180px;top: 8px;\" onClick=\"open_div(" . $subling['indivisual_id'] . ",'" . $subling['gender'] . "','right'); open_only(" . $subling['indivisual_id'] . ",'right');\"></a>
    </div>
    <div id=\"right_form" . $subling['indivisual_id'] . "\" style=\"float:left;\"></div>
                                          
     <!-- left Arrow -->  
    <div id=\"left_aero\" >
    <a id=\"window1_left_arrow\" style=\"left: -16px;top: 10px;\"  onclick=\"open_div(" . $subling['indivisual_id'] . ",'" . $subling['gender'] . "','left'); open_only(" . $subling['indivisual_id'] . ",'left');\" ></a>
    </div>
    <div id=\"left_form" . $subling['indivisual_id'] . "\" style=\"float:left;\"></div> 
    
    <!-- bootom arrow-->  
    <div id=\"down_aero1\">
    <a id=\"window1_down_arrow\" style=\"top: 58px;left: 73px;\"  onclick=\"open_wife_div(" . $subling['indivisual_id'] . ",'bottom','" . $subling['gender'] . "'); open_only(" . $subling['indivisual_id'] . ",'bottom');\" ></a>
    </div>   
    <div id=\"bottom_form" . $subling['indivisual_id'] . "\" style=\"float:left;\"></div> 
  </div>";
        //$div.=get_wife_spouse($subling['indivisual_id'],$left);
        $left = $left - 250;
		
		
    }
    $return[0]=$div;
   $return[1]=$left;  
   $return[2]=$right;
   $return[3]= $wife;	

  return $return;

}

//Get Wife & Spouse /////////////////////////////
function find_wife_spouse($id,$left,$right,$lr_check){
	$wif_rig=$right;
	$aa=1;
	$bb=1;
	$top=268;
	
	$sql=mysql_query("SELECT * FROM `mll_relationships` WHERE `indivisual_1_id`='". $id."' AND `relationship_type_code`='3' ")or die(mysql_error());
	$wife_count=mysql_num_rows($sql);
	while($row=mysql_fetch_array($sql)){
		
	$chl1=get_all_child($id,$row['indivisual_2_id'],$row['indivisual_2_role_code']);

	$countb=($chl/2);
	$countb=$countb+1;
	$l=260*$countb;
	$r=260*$countb;
	$subling=get_allitem('mll_indivisuals','indivisual_id',$row['indivisual_2_id']);
if($lr_check=='ODD'){
	$div.="<div class='window'  id='window".$row['indivisual_2_id']."' style='left:".$left."px; top:".$top."px;  background:url(\"images/";
} else {   $ded=260*$aa;
	$div.="<div class='window' newff=".$aa." id='window".$row['indivisual_2_id']."' style='right:".$right."px; top:".$top."px;  background:url(\"images/";	
}
  if($subling['gender']=="male"){$div.="male_bg";}else{$div.="female_bgb";}
  $div.=".png\") no-repeat;height: 37px;width: 161px;padding: 11px;margin: 0px auto; z-index: 9;'>
  <div id='delete' onClick='deleteuser(".$subling['indivisual_id'].");'><img src='images/x.png' alt='Delete' ></div>
    <!--User Image-->
    ".get_user_image($subling['indivisual_id'],$subling['image'])."
    <!--User Image-->
    <div id=\"name\">
        <a href=\"javascript:void(0)\" title=";
		if($subling['gender']=="male"){$div.="Husband";}else{$div.="Wife";}
		$div.=" style=\"padding:0px;\">".substr($subling['first_name']." ".$subling['last_name'],0,15)."</a><br />
         More <img src=\"images/aero-d.png\" /> 
    </div>
    <!-- right Arrow -->  
    <div id=\"right_aero\" >
    <a id=\"window1_right_arrow\" style=\"left: 180px;top: 8px;\" onClick=\"open_div(". $subling['indivisual_id'].",'". $subling['gender']."','right'); open_only(".$subling['indivisual_id'].",'right');\"></a>
    </div>
    <div id=\"right_form".$subling['indivisual_id']."\" style=\"float:left;\"></div>
                                          
     <!-- left Arrow -->  
    <div id=\"left_aero\" >
    <a id=\"window1_left_arrow\" style=\"left: -16px;top: 10px;\"  onclick=\"open_div(".$subling['indivisual_id'].",'". $subling['gender']."','left'); open_only(". $subling['indivisual_id'].",'left');\" ></a>
    </div>
    <div id=\"left_form".$subling['indivisual_id']."\" style=\"float:left;\"></div> 
    
    <!-- bootom arrow-->  
    <div id=\"down_aero1\">
	<a id=\"window1_down_arrow\" style=\"top: 58px;left: 73px;\" onclick=\"open_div(".$subling['indivisual_id'].",'".$subling['gender']."','bottom'); open_only(".$subling['indivisual_id'].",'bottom');\"></a>
    </div>   
    <div id=\"bottom_form".$subling['indivisual_id']."\" style=\"float:left;\"></div> 
  </div>";
  
  	$son_a=get_son_doughter($id,$row['indivisual_2_id'],$row['indivisual_2_role_code'],($son_l),($Son_r)); 

	$div.=$son_a[0]; 
 
 $aa++; $bb++; $right-=250;}
 
  $return[0]=$left;
  $return[1]=$div;
  $return[2]=$right;	
  $return[3]=$lr_check;
  $return[4]=$wife_count;
  $return[5]=$id;
  return $return;
}

////Get Brother & SIster Connector///////////////
//function find_wife_spouse_conn($id){
//    $con = "";
//    $sql = mysql_query("SELECT * FROM `mll_relationships` WHERE `indivisual_1_id`='" . $id . "' AND `relationship_type_code`='3'") or die(mysql_error());
//    while ($row = mysql_fetch_array($sql)) {
//        //$test=chk_sibling($id);
//        $con.="_addEndpoints(\"window" . $row['indivisual_2_id'] . "\",  [\"TopCenter\", \"BottomCenter\" , \"RightMiddle\" ,\"LeftMiddle\"] , [\"TopCenter\" ,\"RightMiddle\", \"BottomCenter\" ]); jsPlumb.connect({uuids:[\"window" . $id . "RightMiddle\",\"window" . $row['indivisual_2_id'] . "LeftMiddle\"]});\n";
//        $con.=find_wife_spouse_conn($row['indivisual_2_id']);
//    }
//    return $con;
//}

////Get Brother & SIster Connector///////////////
function find_wife_spouse_conn($id){
    $con = "";
    $sql = mysql_query("SELECT * FROM `mll_relationships` WHERE `indivisual_1_id`='" . $id . "' AND `relationship_type_code`='3'") or die(mysql_error());
    while ($row = mysql_fetch_array($sql)) {
        //$test=chk_sibling($id);
        $con.="_addEndpoints(\"window" . $row['indivisual_2_id'] . "\",  [\"TopCenter\", \"BottomCenter\" , \"RightMiddle\" ,\"LeftMiddle\"] , [\"TopCenter\" ,\"RightMiddle\", \"BottomCenter\" ]); jsPlumb.connect({uuids:[\"window" . $id . "RightMiddle\",\"window" . $row['indivisual_2_id'] . "LeftMiddle\"]});\n";
        
    }
    return $con;
}


/////////////////////////////////////////////////////////////
//Get Son and Doughter Div //////////////////////////////////
function get_son_doughter($id,$m_id,$st,$left,$right){
	//$left=300;186
	$cunt=1;
	$left_c=0;
	$richt_c=0; 
	$top=410;
	
//$sql_w=mysql_query("SELECT * FROM `mll_relationships` WHERE `indivisual_1_id`='".$id."' AND `relationship_type_code`=3")or die(mysql_error());

//while($w_row=mysql_fetch_array($sql_w)){
	
	
	if($st==4){//echo $w_row['indivisual_2_role_code'].'ajay<br/>';
	
		 $sql_str="SELECT * FROM `mll_relationships` WHERE ((`indivisual_1_id`='".$id."')or((`indivisual_1_id`='".$m_id."'))) AND `relationship_type_code`=2";
		
		}else{
			$sql_str="SELECT * FROM `mll_relationships` WHERE `indivisual_1_id`='".$m_id."' AND `relationship_type_code`='2'";
		}
	
	
	
	
  //	$sql=mysql_query("SELECT * FROM `mll_relationships` WHERE `indivisual_1_id`='".$id."' AND `relationship_type_code`='2'")or die(mysql_error());
   $sql=mysql_query($sql_str)or die(mysql_error());
	$c_count = mysql_num_rows($sql);
  	while($row=mysql_fetch_array($sql)){ 
	 $lr_check=get_even_odd($cunt);
	
	
	$subling=get_allitem('mll_indivisuals','indivisual_id',$row['indivisual_2_id']);
if($lr_check=="ODD"){
  $div.="<div class='window' id='window".$row['indivisual_2_id']."' style='left:".$left."px; top:".$top."px;  background:url(\"images/";
$left=$left-260;
$left_c++;
	 

}else{
	$div.="<div class='window' id='window".$row['indivisual_2_id']."' style='right:".$right."px; top:".$top."px;  background:url(\"images/";
	
	$right=$right-260;
	$richt_c++;
	}
  if($subling['gender']=="male"){$div.="male_bg";}else{$div.="female_bgb";}
  $div.=".png\") no-repeat;height: 37px;width: 161px;padding: 11px;margin: 0px auto;'>
  <div id='delete' onClick='deleteuser(".$subling['indivisual_id'].");'><img src='images/x.png' alt='Delete' ></div>
    <!--User Image-->
    ".get_user_image($subling['indivisual_id'],$subling['image'])."
    <!--User Image-->
    <div id=\"name\">
        <a href=\"javascript:void(0)\" title=";
		if($subling['gender']=="male"){$div.="Husband";}else{$div.="Wife";}
		$div.=" style=\"padding:0px;\">".substr($subling['first_name']." ".$subling['last_name'],0,15)."</a><br />
         More <img src=\"images/aero-d.png\" /> 
    </div>
    <!-- right Arrow -->  
    <div id=\"right_aero\" >
    <a id=\"window1_right_arrow\" style=\"left: 180px;top: 8px;\" onClick=\"open_div(". $subling['indivisual_id'].",'". $subling['gender']."','right'); open_only(".$subling['indivisual_id'].",'right');\"></a>
    </div>
    <div id=\"right_form".$subling['indivisual_id']."\" style=\"float:left;\"></div>
                                          
     <!-- left Arrow -->  
    <div id=\"left_aero\" >
    <a id=\"window1_left_arrow\" style=\"left: -16px;top: 10px;\"  onclick=\"open_div(".$subling['indivisual_id'].",'". $subling['gender']."','left'); open_only(". $subling['indivisual_id'].",'left');\" ></a>
    </div>
    <div id=\"left_form".$subling['indivisual_id']."\" style=\"float:left;\"></div> 
    
    <!-- bootom arrow-->  
    <div id=\"down_aero1\">
    <a id=\"window1_down_arrow\" style=\"top: 58px;left: 73px;\"  onclick=\"open_wife_div(".$subling['indivisual_id'].",'bottom','".$subling['gender']."'); open_only(".$subling['indivisual_id'].",'bottom');\" ></a>
    </div>   
    <div id=\"bottom_form".$subling['indivisual_id']."\" style=\"float:left;\"></div> 
  </div>";
  //$div.=get_wife_spouse($subling['indivisual_id'],$left);
   $cunt++;}
   $return[0]=$div;
   $return[1]=$left;
   $return[2]=$right;
  $return[3]= $left_c;
  $return[4]= $richt_c;
 $return[5]= $c_count;//}	
  return $return;

}

////Get Son and Daughter Connector////////////////////////////
function get_son_daughter_conn($id) {
    $con = "";
	//
	$sql_w=mysql_query("SELECT * FROM `mll_relationships` WHERE `indivisual_1_id`='".$id."' AND `relationship_type_code`=3")or die(mysql_error());
  if (mysql_num_rows($sql_w)) {
	  
    while($w_row=mysql_fetch_array($sql_w)){
	////
				if($w_row['indivisual_2_role_code']==4){
				
					$sql_str="SELECT * FROM `mll_relationships` WHERE ((`indivisual_1_id`='".$id."')or((`indivisual_1_id`='".$w_row['indivisual_2_id']."'))) AND `relationship_type_code`=2";
					
					}else{
						$sql_str="SELECT * FROM `mll_relationships` WHERE `indivisual_1_id`='".$w_row['indivisual_2_id']."' AND `relationship_type_code`='2'";
					}	
				/////	
	
	

	
  // $sql = mysql_query("SELECT * FROM `mll_relationships` WHERE `indivisual_1_id`='" . $id . "' AND `relationship_type_code`='2'") or die(mysql_error());
  $sql = mysql_query($sql_str) or die(mysql_error());
    while ($row = mysql_fetch_array($sql)) {
        //$test=chk_sibling($id);
        $con.="_addEndpoints(\"window" . $row['indivisual_2_id'] . "\",  [\"TopCenter\", \"BottomCenter\" , \"RightMiddle\" ,\"LeftMiddle\"] , [\"LeftMiddle\" ,\"RightMiddle\", \"BottomCenter\" ]); jsPlumb.connect({uuids:[\"window" . $w_row['indivisual_2_id'] . "BottomCenter\",\"window" . $row['indivisual_2_id'] . "TopCenter\"]});\n";
        //$con.=find_wife_spouse_conn($row['indivisual_2_id']);
    }}}else{
		
        $mother = $id;
   
	////
	
    $sql = mysql_query("SELECT * FROM `mll_relationships` WHERE `indivisual_1_id`='" . $mother . "' AND `relationship_type_code`='2'") or die(mysql_error());
 
    while ($row = mysql_fetch_array($sql)) {
        //$test=chk_sibling($id);
        $con.="_addEndpoints(\"window" . $row['indivisual_2_id'] . "\",  [\"TopCenter\", \"BottomCenter\" , \"RightMiddle\" ,\"LeftMiddle\"] , [\"LeftMiddle\" ,\"RightMiddle\", \"BottomCenter\" ]); jsPlumb.connect({uuids:[\"window" . $mother . "BottomCenter\",\"window" . $row['indivisual_2_id'] . "TopCenter\"]});\n";
		
	}
		}
    return $con;
}

/////////////////////////////////////////////////////////////
///Image Upload Function [ himanshu ]
function get_user_image($id, $img) {
    if ($img == '') {
        $gpg = 'male2.png';
    } else {
        $gpg = $img;
    }
    $img = '<div class="image" id="image' . $id . '" onMouseOver="show(' . $id . ');" onmouseout="hide(' . $id . ')">
<div id="spinner' . $id . '"style="display:none;position:absolute;margin-left: 3px;">
		<img src="spinner_large.gif" border="0">
	</div>
	<img src="uploads/thumb/t_' . $gpg . '"   id="profile_img' . $id . '" style="height:31px;width:40px;"/>
	 <div class="change_button" id="change_button' . $id . '" onClick="change_img(' . $id . ');" >
	<div class="change_button_text"><img src="images/edit.png" style=" background:#FFFFFF;width: 40px; margin-right: 3px;" id="edit_img' . $id . '"></div>
		</div>
		</div>'
    ;
    return $img;
}

function get_parents_image($id, $gender, $img) {
    if ($img == '') {
        if ($gender == 'male') {
            $gpg = 'male1.png';
        } else {
            $gpg = 'femail1.png';
        }
    } else {
        $gpg = $img;
    }
    if ($gender == 'male') {
        $st = '';
        $chstyle = 'style="width: 43px;height: 59px;bottom: 8px;left: 11px;"';
    } else {
        $st = 'float:right';
        $chstyle = 'style="width: 43px;height: 59px;bottom: 7px;left: 150px;"';
    }

    $img = '<div class="image"  style="width: 48px;height: 63px; margin:5px ;' . $st . ';"new="' . $gender . '" id="image' . $id . '" onMouseOver="show(' . $id . ');" onmouseout="hide(' . $id . ')">
<div id="spinner' . $id . '"style="display:none;position:absolute;;margin-left: 7px;margin-top: 12px;">
		<img src="spinner_large.gif" border="0" >
	</div>
	<img src="uploads/thumb/f_' . $gpg . '"   id="profile_img' . $id . '" style="height:63px;width:48px;"/>
	 <div class="change_button" id="change_button' . $id . '" onClick="change_img(' . $id . ',1);" ' . $chstyle . '>
	<div class="change_button_text"><img src="images/edit.png" style=" background:#FFFFFF;width: 40px; margin-right: 3px;" id="edit_img' . $id . '"></div>
		</div>
		</div>'
    ;
    return $img;

    //$result='';
//	$image=get_item('mll_indivisuals','indivisual_id',$id,'image'); //echo $_SESSION['id'];
//    if(empty($image)){
//		$result="<form id='imageform' method='post' enctype='multipart/form-data' action='ajax/image_upload.php'>
//                    <div id='file_browse_wrapper_father' class='image'>
//                    <input type='hidden' value=".$id." name='userid'>
//                        <input type='file' name='file_browse_father' id='file_browse' onchange='this.form.submit();' />
//                    </div>
//                </form>
//                <div id='preview'></div>";
//	}else{
//		$result="<div class='image' >
//                    <img src='uploads/thumb/t_". get_item('faimly_tree','userid',$id,'image')."' width='40' height='31' />
//                </div>";
//	}
//	return $result;
}

/*
  / Function: count2_only
  / Counts All the Records in a table where condition is applies
  / Use:  echo count_only(your_table_name, Where, What);
  / BetaTiger Framework
 */

function count2_only($table_name, $where1, $what1, $where2, $what2) {
    $q = "SELECT * FROM $table_name WHERE $where1 = $what1 AND $where2 = $what2";
    $qr = mysql_query($q) or die("Error: FN Count_all");
    $qn = mysql_num_rows($qr);
    return $qn;
}

function br2nl($string) {
    $return = eregi_replace('<br[[:space:]]*/?' .
            '[[:space:]]*>', chr(13) . chr(10), $string);
    return $return;
}

// Creating Random Number
function createRandomLink() {
    $chars = "abcdefghijkmnopqrstuvwxyz023456789";
    srand((double) microtime() * 1000000);
    $i = 0;
    $pass = '';
    while ($i <= 7) {
        $num = rand() % 33;
        $tmp = substr($chars, $num, 1);
        $pass = $pass . $tmp;
        $i++;
    }
    return $pass;
}

// Image Resizer 1
function make_thumb($img_name, $filename, $new_w, $new_h) {
//get image extension.
    $ext = getExtension($img_name);
//creates the new image using the appropriate function from gd library
    if ((!strcmp("jpg", $ext)) || (!strcmp("jpeg", $ext)) || (!strcmp("gif", $ext)))
        $src_img = imagecreatefromjpeg($img_name);

    if (!strcmp("png", $ext))
        $src_img = imagecreatefrompng($img_name);

    if (!strcmp("gif", $ext))
        $src_img = imagecreatefromgif($img_name);


//gets the dimmensions of the image
    $old_x = imageSX($src_img);
    $old_y = imageSY($src_img);

// next we will calculate the new dimmensions for the thumbnail image
// the next steps will be taken:
// 1. calculate the ratio by dividing the old dimmensions with the new ones
// 2. if the ratio for the width is higher, the width will remain the one define in WIDTH variable
// and the height will be calculated so the image ratio will not change
// 3. otherwise we will use the height ratio for the image
// as a result, only one of the dimmensions will be from the fixed ones
    $ratio1 = $old_x / $new_w;
    $ratio2 = $old_y / $new_h;
    if ($ratio1 > $ratio2) {
        $thumb_w = $new_w;
        $thumb_h = $old_y / $ratio1;
    } else {
        $thumb_h = $new_h;
        $thumb_w = $old_x / $ratio2;
    }
    $dst_img = ImageCreateTrueColor($thumb_w, $thumb_h);

// resize the big image to the new created one
    imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $thumb_w, $thumb_h, $old_x, $old_y);

// output the created image to the file. Now we will have the thumbnail into the file named by $filename
    if (!strcmp("png", $ext))
        imagepng($dst_img, $filename);
    else
        imagejpeg($dst_img, $filename);

//destroys source and destination images.
    imagedestroy($dst_img);
    imagedestroy($src_img);
}

// Image Resizer 2
function getExtension($str) {
    $i = strrpos($str, ".");
    if (!$i) {
        return "";
    }
    $l = strlen($str) - $i;
    $ext = substr($str, $i + 1, $l);
    return $ext;
}

// Getting file extension
function getFileExtension($filename) {
    return substr($filename, strrpos($filename, '.'));
}

////////////////
function validate_user() {
    if (!isset($_SESSION['id']) && ($_SESSION['username'] == "")) {
        header("location:index.php");
    }
}

/**/

function validate_plan() {
    if ((isset($_SESSION['plan_id'])) && (($_SESSION['plan_id'] == NULL) || ($_SESSION['plan_id'] == '') || ($_SESSION['plan_id'] == 0))) {
        header("location:shoppingcart.php");
        exit;
    }
}

////ajay
function setcolomn($table, $where, $what) {
    //echo "SELECT * FROM $table where $where=$what";die();
    $sql2 = mysql_query("SELECT * FROM $table where $where=$what") or die(mysql_error());
    if (isset($sql2)) {

        $ab = 0;
        $aa = mysql_fetch_row($sql2);
        $c = count($aa);
        for ($i = 0; $i < $c; $i++) {
            if (isset($aa[$i]) && ($aa[$i] != NULL) && ($aa[$i] != '0')) {
                $ab = $ab + 1;
            }
        }
    }
    return $ab;
}

////

function insert_user() {
    $chk = mysql_query("SELECT * FROM `mll_indivisuals` WHERE `username`='" . $_POST['username'] . "' or `email`='" . $_POST['email'] . "'") or die(mysql_error());
    if (!mysql_num_rows($chk)) {
        $sql = mysql_query("INSERT INTO `mll_indivisuals` SET `first_name`='" . $_POST['fname'] . "', 
                    `last_name`='" . $_POST['lname'] . "', 
                    `gender`='" . $_POST['gender'] . "', 
                    `username`='" . $_POST['username'] . "', 
                    `password`='" . md5($_POST['password']) . "', 
                    `email`='" . $_POST['email'] . "'
                    ") or die(mysql_error());
        $id = mysql_insert_id();
		mysql_query("UPDATE `mll_indivisuals` SET `family_id`='" .$id. "' ")or die(mysql_error());
        if ($sql) {
            $user = get_allitem('mll_indivisuals', 'indivisual_id', $id);
            $_SESSION['id'] = $id;
            $_SESSION['username'] = $user['username'];
            $_SESSION['first_name'] = $user['first_name'];
            $_SESSION['last_name'] = $user['last_name'];
            $_SESSION['gender'] = $user['gender'];
            $_SESSION['family_id'] = $user['family_id'];
            $_SESSION['plan_id'] = 0;

            send_email_invitation($id, $_POST['password']);
            echo "<script> location='home.php';</script>";
            exit;
        }
    } else {
        echo "<script> jAlert('This Username/Email id Already Exist!', 'MY LIFE LEGACY'); </script>"; /* header("location:index.php"); exit; */
    }
    return $id;
}

  function secondsToTime($seconds)
{
    // extract hours
    $hours = floor($seconds / (60 * 60));
 
    // extract minutes
    $divisor_for_minutes = $seconds % (60 * 60);
    $minutes = floor($divisor_for_minutes / 60);
 
    // extract the remaining seconds
    $divisor_for_seconds = $divisor_for_minutes % 60;
    $seconds = ceil($divisor_for_seconds);
 
    // return the final array
    $obj = array(
        "h" => (int) $hours,
        "m" => (int) $minutes,
        "s" => (int) $seconds,
    );
    return $obj;
}
function return_time($tim){
	$a=($tim['h']*60*60)+($tim['m']*60)+$tim['s'];
	
	if($a<60)
	{
		
		return $a.' seconds';
		}else if($a<(60*60)){
			
			return round($a/60).' minutes';
			
			}else if($a<(60*60*24)){
				
				   
					return round($a/3600).' hours';
				
				}else if($a<(60*60*24*7)){
				
				   $a = $a/3600;
					return round($a/24).' days';
				
				}
				else if($a<(60*60*24*30)){
					$a = $a/3600;
				   $a = $a/24;
					return round($a/4).' weeks';
				
				}
	
	
	//if($tim['h']==0)
//	{
//		if($tim['m']<60)
//		{
//		  return $tim['m'].' minutes';
//		}else
//		{
//		   return $tim['s'].' seconds';
//		}
//	}
//	else
//	{
//		return $tim['h'].' hours';
//	}
}
function login() { ///print_r($_POST); die();
    $result = mysql_query("SELECT * FROM `mll_indivisuals` WHERE username='" . mysql_real_escape_string(trim($_POST['username'])) . "' AND password='" . md5(mysql_real_escape_string(trim($_POST['password']))) . "'") or die(mysql_error());

    if (mysql_num_rows($result)) {
        $row = mysql_fetch_array($result);
        $_SESSION['id'] = $row['indivisual_id'];
        $_SESSION['username'] = $row['username'];
        $_SESSION['first_name'] = $row['first_name'];
        $_SESSION['last_name'] = $row['last_name'];
        $_SESSION['gender'] = $row['gender'];
		$_SESSION['family_id'] =$row['family_id'];
        
		echo "<script> location='home.php';</script>";
    } else {
        echo "<script> jAlert('Invalid Username & Password!', 'MY LIFE LEGACY');</script>";
    }
}

/// for send mail //////////////////

function send_email_invitation($id, $pass = true) {
    $user = mysql_fetch_array(mysql_query("SELECT * FROM `mll_indivisuals` WHERE `indivisual_id`='$id'"));
    $first_name1 = $user['first_name'];
    $last_name1 = $user['last_name'];
    $userName1 = $user['username'];
    $to = $user['email'];
    $password1 = $pass;
    if (!empty($to)) {
        $app_name1 = get_item('az_cpanel', 'cpid', 1, 'app_name');

        $mail_row = get_allitem('az_emails', 'emid', 1);
        $subject = $mail_row["emsubject"];
        $message = $mail_row["emcontent"];
        $from_feild = $mail_row["relemailid"];
        $from_name = $mail_row["emfrom"];

        $placeholder = array('$first_name', '$last_name', '$app_name', '$loginemail', '$password');
        $values = array($first_name1, $last_name1, $app_name1, $userName1, $password1);
        $message = str_replace($placeholder, $values, $message);
        $headers = "MIME-Version: 1.0" . "\r\n";
        $headers .= "Content-type:text/html; charset=iso-8859-1" . "\r\n";
        $headers.= 'From:' . $from_name . '<' . $from_feild . ">\n" .
                'Reply-To: ' . $from_feild . "\n";
        @mail($to, $subject, $message, $headers);
    }
}

function get_even_odd($num) {
    $n = $num % 2;
    if ($n == 0) {
        return 'EVEN';
    } else {
        return 'ODD';
    }
}

function insertBilling() {
    $sql = mysql_query("INSERT INTO `az_billing` SET 
         `userid`='" . $_POST['indivisual_id'] . "', 
    `plan_type`='" . $_POST['amount'] . "',  
    `promocode`='" . $_POST['promocode'] . "',
    `first_name`='" . $_POST['x_first_name'] . "', 
    `last_name`='" . $_POST['x_last_name'] . "', 
    `address`='" . $_POST['x_address'] . "', 
    `address2`='" . $_POST['x_address2'] . "', 
    `city`='" . $_POST['x_city'] . "', 
    `state` = '" . $_POST['x_state'] . "' ,                       
    `zip` = '" . $_POST['x_zip'] . "', 
    `country`='" . $_POST['x_country'] . "', 
    `email`='" . $_POST['x_email'] . "',
    `phone_no`='" . $_POST['x_phone'] . "',
    `card_no`='" . $_POST['x_card_num'] . "', 
    `x_exp_date`='" . $_POST['x_exp_date_month'] . " / " . $_POST['x_exp_date_year'] . "', 
    `cvv`='" . $_POST['x_card_code'] . "',
    `chkPolicy`='" . $_POST['chkPolicy'] . "',
    `hear_from`='" . $_POST['hear_from'] . "'
    ") or die(mysql_error());
}
//////////////////
 ///////////////
 function get_bro2($id){$bro = '';
			       $chk=mysql_query("SELECT * FROM `mll_relationships` WHERE `indivisual_1_id`='".$id."' AND `relationship_type_code`=4")or die(mysql_error());
			    while($row=mysql_fetch_array($chk)) { $bro.=$row['indivisual_2_id'];
				                                      $bro.=",".get_bro2($row['indivisual_2_id']); }
				         return $bro;}
///////////						 
	function all_b($id,$fid){
		     $bro=array();
		     $farr2= get_bro($fid).$fid;
		     $farr= explode("," ,$farr2);
		      $new_array_without_nulls = array_filter($farr);
		       foreach($new_array_without_nulls as $val){if($val!=$id){$bro[]=$val;} }
		     	return $bro;} 
//////
 function get_all_child($f_id,$m_id,$st){
	 if($st==4){
		 $str="SELECT * FROM `mll_relationships` WHERE ((`indivisual_1_id`='".$f_id."')or((`indivisual_1_id`='".$m_id."'))) AND `relationship_type_code`=2";
		 }else{
			  $str="SELECT * FROM `mll_relationships` WHERE `indivisual_1_id`='".$m_id."' AND `relationship_type_code`=2";
			 
			 }
	   $chk=mysql_query($str)or die(mysql_error());
	  $ac= mysql_num_rows($chk);
	   while($row=mysql_fetch_array($chk)) { $bro.=$row['indivisual_2_id'];
			                                   // echo $row['indivisual_2_id'].'<br/>'; 
												}
				       return $ac;
	 }

?>